﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using Omu.ProgrammerTimer.Properties;

namespace Omu.ProgrammerTimer
{
    public partial class Form1 : Form
    {
        enum State
        {
            Working,
            Resting, 
            Stopped
        }

        private State _state;

        private readonly int _workInterval = 5000;

        private readonly int _restInterval = 10000;

        private readonly Stopwatch _stopwatch = new Stopwatch();

        public Form1()
        {
            InitializeComponent();
            TopMost = true;
            StartPosition = FormStartPosition.Manual;
            var y = Screen.PrimaryScreen.WorkingArea.Height - Height - 45;
            Location = new Point(35, y);

            _workInterval = Settings.Default.WorkingMinutes * 1000 * 60;
            _restInterval = Settings.Default.RestingMinutes * 1000 * 60;
        }

        private void Form1Load(object sender, EventArgs e)
        {
            _workRestTimer.Tick += (s, a) =>
            {
                _stopwatch.Restart();

                if (_state == State.Working)
                    Rest();
                else if(_state == State.Resting)
                    Work();
            };

            updateNumbersTimer.Tick += (s, a) =>
            {
                if (_state == State.Stopped)
                {
                    label1.Text = "Stopped";
                    notifyIcon1.Text = "Stopped";
                }
                else
                {
                    var elapsed = _stopwatch.Elapsed;
                    var totalSecRem = (int)(((_state == State.Working ? _workInterval : _restInterval) - elapsed.TotalMilliseconds) / 1000);

                    var totalMinutesRem = totalSecRem / 60;
                    var totalHoursRem = totalMinutesRem / 60;

                    var seconds = totalSecRem % 60;
                    var minutes = totalMinutesRem % 60;
                    var hours = totalHoursRem % 24;
                    var days = totalHoursRem / 24 > 0 ? "*" : "";

                    label1.Text = string.Format("{0} {1:00}:{2:00}:{3:00}", days, hours, minutes, seconds);
                    notifyIcon1.Text = string.Format("Programmer Timer - {0} {1}", _state.ToString(), label1.Text);
                }
            };

            updateNumbersTimer.Start();
            Start();
        }

        void Rest()
        {
            _state = State.Resting;
            _workRestTimer.Interval = _restInterval;
            _workRestTimer.Start();
            ShowWindow();
        }

        void Work()
        {
            _state = State.Working;
            _workRestTimer.Interval = _workInterval;
            _workRestTimer.Start();
            HideWindow();
        }

        private void Start()
        {
            _state = State.Resting;
            startStopToolStripMenuItem.Text = "Stop";
            _workRestTimer.Start();
            _stopwatch.Restart();
        }

        private void StopStart()
        {
            if (_state == State.Stopped)
            {
                Start();
            }
            else
            {
                _state = State.Stopped;
                _workRestTimer.Stop();
                _stopwatch.Stop();
                startStopToolStripMenuItem.Text = "Start";
            }
        }

        private void NotifyIcon1MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if(WindowState == FormWindowState.Normal)
            {
                HideWindow();
            }
            else
            {
                ShowWindow();
            }
        }

        private void Form1Resize(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
            {
                HideWindow();
            }
        }

        private void PauseToolStripMenuItemClick(object sender, EventArgs e)
        {
            StopStart();
        }

        private void ExitMenuItem1Click(object sender, EventArgs e)
        {
            Close();
        }

        private void HideMenuItemClick(object sender, EventArgs e)
        {
            HideWindow();
        }

        private void OpenMenuItemClick(object sender, EventArgs e)
        {
            ShowWindow();
        }

        private void Form1Click(object sender, EventArgs e)
        {
            HideWindow();
        }

        private void Label1Click(object sender, EventArgs e)
        {
            HideWindow();
        }

        private void HideWindow()
        {
            Hide();
            WindowState = FormWindowState.Minimized;
        }

        private void ShowWindow()
        {
            Show();
            WindowState = FormWindowState.Normal;
        }
    }
}
